package com.minjiang.system.service.sec.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.minjiang.model.system.FoodDetailType;
import com.minjiang.model.system.FoodType;
import com.minjiang.model.system.MyUser;
import com.minjiang.model.vo.FoodTypeVo;
import com.minjiang.system.mapper.sec.FoodDetailTypeMapper;
import com.minjiang.system.service.sec.FoodDetailTypeService;
import com.minjiang.system.service.sec.FoodTypeService;
import com.minjiang.system.mapper.sec.FoodTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【food_type】的数据库操作Service实现
* @createDate 2022-12-18 21:18:43
*/
@Service
public class FoodTypeServiceImpl extends ServiceImpl<FoodTypeMapper, FoodType>
    implements FoodTypeService{
    @Autowired
    private FoodTypeMapper foodTypeMapper;
    @Autowired
    private FoodDetailTypeMapper foodDetailTypeMapper;
    @Override
    public IPage<FoodType> selectPage(Page<FoodType> pageParam, FoodTypeVo foodTypeVo) {
        String title = foodTypeVo.getKeyword();

        //封装参数
        QueryWrapper<FoodType> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(title)) {
            wrapper.like("type_name",title);
        }
        //调用mapper方法实现分页条件查询
        IPage<FoodType> foodTypeIPage = foodTypeMapper.selectPage(pageParam, wrapper);
        List<FoodType> foodTypes = new ArrayList<>();
        foodTypeIPage.getRecords().forEach(item->{
            LambdaQueryWrapper<FoodDetailType> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(FoodDetailType::getFoodTypeId,item.getId());
            List<FoodType> collect = foodDetailTypeMapper.selectList(queryWrapper).stream().map(res -> new FoodType(res.getId(), res.getTypeName())).collect(Collectors.toList());
            item.setChildren(collect);
        });
        return foodTypeIPage;
    }

    @Override
    public List<FoodType> selectList() {
       return foodTypeMapper.selectList(null);
    }
}




